package de.deliancourt.jarvis.implementations;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.media.j3d.Appearance;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;

import com.sun.j3d.utils.geometry.Primitive;

public class WindowSide extends Primitive {
	
	public static final Integer YZ=0;
	public static final Integer XY=1;
	public static final Integer XZ=2;
	
	private Integer _Rows=1;
	private Integer _Columns=1;
	private Shape3D shape = new Shape3D();
	private ArrayList<Shape3D> shapes = new ArrayList<Shape3D>();
	private ArrayList<Tile> tiles = new ArrayList<Tile>();
	private ArrayList<TransformGroup> tGroups = new ArrayList<TransformGroup>();
	
	public WindowSide(Point3d TopLeft, Point2d WHD, Integer Axis, Integer Rows, Integer Columns ){
		double tileWidth = 0;
		double tileHeight = 0;
		double tileDepth = 0;

		switch (Axis){
		case 1: tileHeight = WHD.y /Rows;  tileWidth = WHD.x / Columns; break;
		case 2: tileWidth = WHD.x /Columns; tileDepth = WHD.y / Rows; break;
		default:tileHeight = WHD.x /Rows; tileDepth = WHD.y / Columns;
		}
		_Rows=Rows;
		_Columns=Columns;

		for (Integer Row=0;Row<Rows;Row++){
			for(Integer Column=0;Column<Columns;Column++){
				Tile t = null;
				switch (Axis){
				case 1: t = new Tile(TopLeft.x+(tileWidth*Column), TopLeft.y-(tileHeight*Row), TopLeft.z, tileWidth,tileHeight, Axis); break;
				case 2: t = new Tile(TopLeft.x+(tileWidth*Column), TopLeft.y, TopLeft.z-(tileDepth*Row), tileWidth,tileDepth, Axis); break;
				default:t = new Tile(TopLeft.x, TopLeft.y-(tileHeight*Row), TopLeft.z-(tileDepth*Column), tileHeight,tileDepth, Axis);
				}
				
				shapes.add(t.getShape(0));
				tiles.add(t);
				
				tGroups.add(t.gettGroup());
				// this.addChild(t.getShape(0));
			}
		}
	}

	public WindowSide(Point3d TopLeft, Point2d WHD, Integer Axis ){
		this(TopLeft,WHD,Axis,4,3);
	}

	public ArrayList<Shape3D> getAllShapes(){
		return shapes;
	}
	
	public ArrayList<Tile> getAllTiles(){
		return tiles;
	}
	
	public Tile GetTile(Integer pos){
		return tiles.get(pos);
	}
	public ArrayList<TransformGroup> getAlltGroups() {
		return tGroups;
	}
	
	public TransformGroup gettGroup(Integer Pos){
		return tGroups.get(Pos); 
	}
	
	public Tile GetTile(Integer Row, Integer Column ){
		return tiles.get(((Row*_Columns)+Column)-1);
	}
	
	@Override
	public Appearance getAppearance(int Pos) {
		if(Pos<0 || Pos>=_Rows*_Columns) return null;
		return shapes.get(Pos).getAppearance();
	}

	@Override
	public Shape3D getShape(int Pos) {
		if(Pos<0 || Pos>=_Rows*_Columns) return null;
		return shapes.get(Pos);
	}

	@Override
	public void setAppearance(Appearance App) {
		for (int i = 0;i<_Rows*_Columns;i++){
			shapes.get(i).setAppearance(App);
		}

	}

}
